package com.itawei.d5_threadsafe;

public class MyThread extends Thread {
    static int ticket = 0;
    Object lock = new Object();
    @Override
    public void run() {

        synchronized (lock){
            while (true) {
                if (ticket < 100) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName() + "正在卖第" + ticket + "张花票！");
                }else{
                    break;
                }
            }
        }
    }
}
